const {
	reqGet,
	reqPost,
} = require('@utils/request')
const {
	loadProjectData,
	loadAPIDetail,
} = require('./api-data-2x')
const {
	diffHistory,
} = require('./api-utils-history-2x')

let _pageNumber = 1

module.exports = {

	/**
	 * 查看版本信息列表
	 * @return {[type]} [description]
	 */
	checkVersionMethod() {
		let {
			store,
			versionTable,
			versionDialog,
		} = this
		$.messager.progress()
		reqPost('/ult/project/version', {
			project_id: store.projectId,
		}).then(result => {
			versionTable.datagrid('loadData', result)
			versionDialog.dialog('open');
			$.messager.progress('close')
		})
	},

	/**
	 * 查看版本详细信息
	 * @return {[type]} [description]
	 */
	loadVersionMethod() {
		let {
			store,
			btnEdit,
			versionTable,
			versionDialog,
			btnVersionBack,
		} = this
		let selected = versionTable.datagrid('getSelected')
		if (store.version == selected.version) {
			versionDialog.dialog('close')
			return false
		}
		loadProjectData(this, selected.project_id, selected)
		versionDialog.dialog('close')
		this.store.isHistoryVersion = true
		btnEdit.linkbutton('disable')
		btnVersionBack.linkbutton('enable')
	},

	/**
	 * 返回最新版本
	 * @return {[type]} [description]
	 */
	backVersionMethod() {
		let {
			store,
			btnEdit,
			btnVersionBack,
		} = this
		loadProjectData(this, store.projectId)
		btnEdit.linkbutton('enable')
		btnVersionBack.linkbutton('disable')
	},

	onDblClickRow(index, row) {
		let {
			versionTable,
			versionHistoryDialog,
		} = this
		let historyHTML = ''
		let info = ''
		try {
			let value = row.description
			value = JSON.parse(value)
			historyHTML = value.historyHTML
			info = value.info || ''
			if (!historyHTML) {
				// 版本最终数据
				let editProjectData = row.project_data;
				editProjectData = JSON.parse(editProjectData);
				console.log('value.historyJSON', value.historyJSON);
				// 修改记录
				historyHTML = diffHistory({
					// 版本修改前数据(上一个版本数据)
					orgProjectData: value.orgProjectData,
					// 版本历史记录
					history: value.historyJSON,
					// 该版本最终数据
					editProjectData,
				})
			}
		} catch (e) {
			console.log(e)
			historyHTML = row.description
		}
		if (historyHTML)
			historyHTML = `<div style="font-size:18px;">${historyHTML}</div>`
		else
			historyHTML = '<div style="font-size:18px;color:red;">这个版本没有变化！</div>'
		if (info)
			historyHTML = `<div style="font-size:18px;">提交说明：${info}</div><hr>${historyHTML}`
		versionHistoryDialog.dialog({
			top: 100,
			left: 300,
			width: 800,
			height: 460,
			content: historyHTML,
			modal: true,
			closed: false,
		})
	},

	/**
	 * 分页控制，选择页码
	 * @param  {[type]} pageNumber [description]
	 * @param  {[type]} pageSize   [description]
	 * @return {[type]}            [description]
	 */
	onSelectPage(pageNumber, pageSize) {
		let {
			store,
			versionTable,
			versionDialog,
		} = this
		_pageNumber = pageNumber
		reqPost('/ult/project/version', {
			project_id: store.projectId,
			page: pageNumber,
			rows: pageSize,
		}).then(result => {
			versionTable.datagrid('loadData', result)
		})
	},
}